from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from assets.models import (
    Organization, Department, Location, AssetCategory, 
    Warehouse, Supplier, Consumable
)


class Command(BaseCommand):
    help = '初始化测试数据'

    def handle(self, *args, **options):
        self.stdout.write(self.style.SUCCESS('开始初始化测试数据...'))
        
        # 创建组织架构
        org, created = Organization.objects.get_or_create(
            name='测试公司',
            defaults={'code': 'TEST001'}
        )
        if created:
            self.stdout.write('创建组织：测试公司')
        
        dept, created = Department.objects.get_or_create(
            name='IT部门',
            defaults={'code': 'IT001', 'organization': org}
        )
        if created:
            self.stdout.write('创建部门：IT部门')
        
        location, created = Location.objects.get_or_create(
            name='总部大楼',
            defaults={'address': '北京市朝阳区建国路1号'}
        )
        if created:
            self.stdout.write('创建位置：总部大楼')
        
        # 创建资产类别
        category, created = AssetCategory.objects.get_or_create(
            name='办公用品',
            defaults={'code': 'CAT001'}
        )
        if created:
            self.stdout.write('创建类别：办公用品')
        
        # 创建仓库
        warehouse, created = Warehouse.objects.get_or_create(
            name='主仓库',
            defaults={
                'location': location,
                'manager': User.objects.first(),
                'description': '公司主要的储存仓库'
            }
        )
        if created:
            self.stdout.write('创建仓库：主仓库')
        
        # 创建供应商
        supplier, created = Supplier.objects.get_or_create(
            name='办公用品供应商',
            defaults={
                'contact_person': '张经理',
                'phone': '13800138000',
                'email': 'zhang@supplier.com',
                'address': '北京市朝阳区建国路88号'
            }
        )
        if created:
            self.stdout.write('创建供应商：办公用品供应商')
        
        # 创建测试耗材
        test_consumables = [
            {
                'name': '打印纸A4',
                'code': 'PAPER001',
                'quantity': 5,  # 当前库存很低
                'min_quantity': 20,  # 最小库存
                'unit': '包',
                'purchase_price': 25.00
            },
            {
                'name': '签字笔',
                'code': 'PEN001',
                'quantity': 0,  # 缺货
                'min_quantity': 50,
                'unit': '支',
                'purchase_price': 2.50
            },
            {
                'name': '订书机',
                'code': 'STAPLER001',
                'quantity': 8,  # 库存较低
                'min_quantity': 5,
                'unit': '个',
                'purchase_price': 35.00
            },
            {
                'name': '文件夹',
                'code': 'FOLDER001',
                'quantity': 80,  # 库存正常
                'min_quantity': 20,
                'unit': '个',
                'purchase_price': 8.00
            },
            {
                'name': '胶带',
                'code': 'TAPE001',
                'quantity': 2,  # 低库存
                'min_quantity': 10,
                'unit': '卷',
                'purchase_price': 5.00
            }
        ]
        
        for consumable_data in test_consumables:
            consumable, created = Consumable.objects.get_or_create(
                code=consumable_data['code'],
                defaults={
                    'name': consumable_data['name'],
                    'category': category,
                    'warehouse': warehouse,
                    'supplier': supplier,
                    'quantity': consumable_data['quantity'],
                    'min_quantity': consumable_data['min_quantity'],
                    'unit': consumable_data['unit'],
                    'purchase_price': consumable_data['purchase_price']
                }
            )
            if created:
                self.stdout.write(f'创建耗材：{consumable_data["name"]} (库存: {consumable_data["quantity"]})')
            else:
                # 更新库存数量以便测试
                consumable.quantity = consumable_data['quantity']
                consumable.save()
                self.stdout.write(f'更新耗材库存：{consumable_data["name"]} (库存: {consumable_data["quantity"]})')
        
        self.stdout.write(self.style.SUCCESS('测试数据初始化完成！'))
